package com.lejin.portal.service.impl;

import cn.hutool.json.JSONUtil;

import com.lejin.api.cms.CMSApi;
import com.lejin.cms.entity.Content;
import com.lejin.item.entity.CourseOutline;
import com.lejin.portal.service.IPortalService;
import com.lejin.vo.CatCascadeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class PortalService implements IPortalService {


    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private CMSApi cMSApi;


    /**
     * 广告内容管理（轮播图）
     * 用hash类型存
     * key=> "menhu:portal:cms"
     * field=>typeid
     * value=>[]
     *
     * @param tpId
     * @return
     */
    @Override
    public List<Content> findContentListByTpid(String tpId) {
        BoundHashOperations<String, Object, Object> operations = stringRedisTemplate.boundHashOps("menhu:portal:cms");

        Object contentListJson = operations.get(tpId);

        if(StringUtils.isEmpty(contentListJson)){
            //redis中没有，去数据库拿
            List<Content> contentList = cMSApi.findContentListByTpid(tpId);
            //往redis中存一份
            operations.put(tpId, JSONUtil.toJsonStr(contentList));  ////field=>tpId,value=>JSONUtil.toJsonStr(contentList)

            return contentList;
        }

        //redis中有，做个反序列化
        List<Content> contents = JSONUtil.toList((String) contentListJson, Content.class);
        return contents;
    }

    /**
     * 二级分类菜单数据
     * 用string类型存
     * key=> "menhu:portal:catmenu"
     * value=>[]
     * @return
     */
    @Override
    public List<CatCascadeVO> findCatMenu() {

        BoundValueOperations<String, String> operations = stringRedisTemplate.boundValueOps("menhu:portal:catmenu");

        String catMenuJson = operations.get();

        if(StringUtils.isEmpty(catMenuJson)){
            //redis中没有，去数据库拿
            List<CatCascadeVO> catMenu = cMSApi.findCat();
            //往redis中存一份
            operations.set(JSONUtil.toJsonStr(catMenu));
            return catMenu;
        }
        List<CatCascadeVO> catMenu = JSONUtil.toList(catMenuJson, CatCascadeVO.class);

        return catMenu;
    }

    /**
     * 缓存一级分类下的最低4个价格课程信息
     * 用hash类型存
     * key=> "menhu:portal:cat1course"
     * field=> ${catid}
     * value=> CourseOutline
     * @return
     */
    @Override
    public List<CourseOutline> findCourseByPriceAsc(String catId) {
        BoundHashOperations<String, Object, Object> operations = stringRedisTemplate.boundHashOps("menhu:portal:cat1course");

        Object courseOutlineListJson = operations.get(catId);

        if(StringUtils.isEmpty(courseOutlineListJson)){
            //redis中没有，去数据库拿
            List<CourseOutline> courseOutlineList = cMSApi.findCourseByPriceAsc(catId);
            //往redis中存一份
            operations.put(catId, JSONUtil.toJsonStr(courseOutlineList));  ////field=>tpId,value=>JSONUtil.toJsonStr(contentList)

            return courseOutlineList;
        }

        //redis中有，做个反序列化
        List<CourseOutline> courseOutlines = JSONUtil.toList((String) courseOutlineListJson, CourseOutline.class);
        return courseOutlines;
    }


    /**
     * 缓存首页热门课程信息
     * 用hash类型存
     * key=> "menhu:portal:hotcourse"
     * field=> ${cat2id}
     * value=> CourseOutline
     * @return
     */
    @Override
    public List<CourseOutline> findCourseByHitsDesc(String cat2Id) {
        BoundHashOperations<String, Object, Object> operations = stringRedisTemplate.boundHashOps("menhu:portal:hotcourse");

        Object courseOutlineListJson = operations.get(cat2Id);

        if(StringUtils.isEmpty(courseOutlineListJson)){
            //redis中没有，去数据库拿
            List<CourseOutline> courseOutlineList = cMSApi.findCourseByHitsDesc(cat2Id);
            //往redis中存一份
            operations.put(cat2Id, JSONUtil.toJsonStr(courseOutlineList));  ////field=>tpId,value=>JSONUtil.toJsonStr(contentList)

            return courseOutlineList;
        }

        //redis中有，做个反序列化
        List<CourseOutline> courseOutlines = JSONUtil.toList((String) courseOutlineListJson, CourseOutline.class);
        return courseOutlines;
    }

}
